/*
 * Copyright (c) 2012 SUBSECRETARIA DEFENSORIA DEL ESPACIO PUBLICO - SDEP.
 * All Rights Reserved.
 * This software is the confidential and proprietary information of SDEP
 * Developed By CONSTULT-SOFT S.A, Development Team 2012.
 *
 */
package co.gov.medellin.sdep.sisdep.domain.operativa;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

import co.gov.medellin.sdep.sisdep.operativa.exception.ComparendoNegReinException;
import co.gov.medellin.sdep.sisdep.operativa.exception.ComparendoNegReinRuntimeException;
import co.gov.medellin.sdep.sisdep.operativa.service.IComparendoNegReinService;
import co.gov.medellin.sdep.sisdep.common.AppConstants;
import co.gov.medellin.sdep.sisdep.domain.MessageModalWindowController;
import co.gov.medellin.sdep.sisdep.domain.BaseController;
import co.gov.medellin.sdep.sisdep.model.ComparendoNegRein;

/**
 * @author Usuario
 * 
 */
public class ComparendoNegReinController extends BaseController implements
		Serializable {

	/**
	 * generated serial version
	 */
	private static final long serialVersionUID = -3285110767384990403L;

	/**
	 * Constructor por defecto, inicializa los elemento basicos del Controlador
	 */
	public ComparendoNegReinController() {
		this.comparendoNegReinListModel = new ListDataModel();
	}

	/** bean para el manejo del formulario de busqueda ComparendoNegRein */
	public ComparendoNegRein comparendoNegReinSearch = new ComparendoNegRein();

	/** Objeto contenedor del listado de ComparendoNegRein luego de una consulta. */
	private DataModel comparendoNegReinListModel;

	/** bean para el manejo del formulario de creacion/edicion. ComparendoNegRein */
	private ComparendoNegRein comparendoNegRein = new ComparendoNegRein();

	/** instancia de servicio comparendoNegReinService */
	private IComparendoNegReinService comparendoNegReinService;

	// --------------------------------------------------------------------
	// --- metodos para atender acciones init()
	// --------------------------------------------------------------------

	/**
	 * 
	 * @return String
	 */
	public String searchComparendoNegReinByKey() {
		String msg = "";
		try {
			List valores = null;
			valores = getComparendoNegReinService().load(1, 10,
					this.comparendoNegReinSearch);
			this.comparendoNegReinListModel.setWrappedData(valores);
		} catch (Exception e) {
			msg = e.getMessage();
			e.printStackTrace(System.err);
		}
		if (this.comparendoNegReinListModel.getWrappedData() == null) {
			msg = AppConstants.NO_RECORDS_FOUND;
		} else {
			if (((List) this.comparendoNegReinListModel.getWrappedData()).isEmpty()) {
				msg = AppConstants.NO_RECORDS_FOUND;
			}
		}
		((MessageModalWindowController) getBean("messageController"))
				.setSuccessMessage(msg, null, null);
		return AppConstants.COMPARENDO_NEG_REIN_SEARCH_VIEW;
	}
	
	/**
	 * 
	 * @return String
	 */
	public String searchComparendoNegReinByRadicado() {
		String msg = "";
		try {
			List valores = null;
			valores = getComparendoNegReinService().loadByRadicado(1, 10,
					this.comparendoNegReinSearch);
			this.comparendoNegReinListModel.setWrappedData(valores);
		} catch (Exception e) {
			msg = e.getMessage();
			e.printStackTrace(System.err);
		}
		if (this.comparendoNegReinListModel.getWrappedData() == null) {
			msg = AppConstants.NO_RECORDS_FOUND;
		} else {
			if (((List) this.comparendoNegReinListModel.getWrappedData()).isEmpty()) {
				msg = AppConstants.NO_RECORDS_FOUND;
			}
		}
		((MessageModalWindowController) getBean("messageController"))
				.setSuccessMessage(msg, null, null);
		return AppConstants.COMPARENDO_NEG_REIN_SEARCH_VIEW;
	}
	
	public String searchComparendoNegReinByRadicado(IComparendoNegReinService service) {
		String msg = "";
		try {
			List valores = null;
			valores = service.loadByRadicado(1, 10,
					this.comparendoNegReinSearch);
			this.comparendoNegReinListModel.setWrappedData(valores);
		} catch (Exception e) {
			msg = e.getMessage();
			e.printStackTrace(System.err);
		}
		if (this.comparendoNegReinListModel.getWrappedData() == null) {
			msg = AppConstants.NO_RECORDS_FOUND;
		} else {
			if (((List) this.comparendoNegReinListModel.getWrappedData()).isEmpty()) {
				msg = AppConstants.NO_RECORDS_FOUND;
			}
		}
		((MessageModalWindowController) getBean("messageController"))
				.setSuccessMessage(msg, null, null);
		return AppConstants.COMPARENDO_NEG_REIN_SEARCH_VIEW;
	}	

	/**
	 * 
	 * @return
	 */
	public String create() {
		String msg = AppConstants.DEFAULT_ERROR;
		try {
			if (this.comparendoNegRein.getId().longValue() == 0L) {
				if(comparendoNegRein.isCompromisoPagoChk())
					comparendoNegRein.setCompromisoPago("S");
				else
					comparendoNegRein.setCompromisoPago("N");
				getComparendoNegReinService().insert(this.comparendoNegRein);
				msg = AppConstants.SUCCESS_CREATE;
			}
		} catch (ComparendoNegReinException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		} catch (ComparendoNegReinRuntimeException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		}
		((MessageModalWindowController) getBean("messageController"))
				.setSuccessMessage("- comparendoNegRein " + msg, null, null);
		return AppConstants.COMPARENDO_NEG_REIN_SEARCH_VIEW;
	}

	/**
	 * 
	 * @return
	 */
	public String update() {
		String msg = AppConstants.DEFAULT_ERROR;
		try {
			if (this.comparendoNegRein.getId().longValue() != 0L) {
				if(comparendoNegRein.isCompromisoPagoChk())
					comparendoNegRein.setCompromisoPago("S");
				else
					comparendoNegRein.setCompromisoPago("N");

				getComparendoNegReinService().update(this.comparendoNegRein);
				msg = AppConstants.SUCCESS_UPDATE;
			}
		} catch (ComparendoNegReinException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		} catch (ComparendoNegReinRuntimeException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		}
		((MessageModalWindowController) getBean("messageController"))
				.setSuccessMessage("- Valor Tipo Maestra " + msg, null, null);
		return AppConstants.COMPARENDO_NEG_REIN_SEARCH_VIEW;
	}

	// --------------------------------------------------------------------
	// --- metodos para atender acciones end()
	// --------------------------------------------------------------------

	// --------------------------------------------------------------------
	// --- metodos para atender reglas de navegacion init()
	// --------------------------------------------------------------------

	/**
	 * 
	 */
	public String createComparendoNegRein() {
		comparendoNegRein = new ComparendoNegRein();
		comparendoNegRein.setId(new Long(0L));
		comparendoNegRein.setCodigo("");
		comparendoNegRein.setIdComparendo(comparendoNegReinSearch.getIdComparendo());
		//comparendoNegRein.setValor("");
		//comparendoNegRein.setActivo("N");
		return AppConstants.COMPARENDO_NEG_REIN_CREATE_VIEW;
	}

	/**
	 * 
	 * @return
	 */
	public String updateComparendoNegRein() {
		Map params = getExternalContext().getRequestParameterMap();
		String comparendoNegReinDataIndex = (String) params
				.get("comparendoNegReinDataIndex");
		this.comparendoNegRein = (ComparendoNegRein) ((List) this.comparendoNegReinListModel
				.getWrappedData())
				.get(Integer.parseInt(comparendoNegReinDataIndex));
		return AppConstants.COMPARENDO_NEG_REIN_UPDATE_VIEW;
	}

	/**
	 * 
	 * @return
	 */
	public String cancelComparendoNegRein() {
		return AppConstants.COMPARENDO_NEG_REIN_SEARCH_VIEW;
	}

	// --------------------------------------------------------------------
	// --- metodos para atender reglas de navegacion end()
	// --------------------------------------------------------------------

	// --------------------------------------------------------------------
	// --- getters and setters init()
	// --------------------------------------------------------------------

	/**
	 * 
	 */
	public ComparendoNegRein getComparendoNegReinSearch() {
		return comparendoNegReinSearch;
	}

	/**
	 * 
	 * @param comparendoNegReinSearch
	 */
	public void setcomparendoNegReinSearch(ComparendoNegRein comparendoNegReinSearch) {
		this.comparendoNegReinSearch = comparendoNegReinSearch;
	}

	/**
	 * 
	 * @return
	 */
	public DataModel getcomparendoNegReinListModel() {
		return comparendoNegReinListModel;
	}

	/**
	 * 
	 * @param comparendoNegReinListModel
	 */
	public void setcomparendoNegReinListModel(DataModel comparendoNegReinListModel) {
		this.comparendoNegReinListModel = comparendoNegReinListModel;
	}

	/**
	 * 
	 * @return
	 */
	public ComparendoNegRein getComparendoNegRein() {
		return comparendoNegRein;
	}

	/**
	 * 
	 * @param comparendoNegRein
	 */
	public void setcomparendoNegRein(ComparendoNegRein comparendoNegRein) {
		this.comparendoNegRein = comparendoNegRein;
	}

	/**
	 * obtencion del servicio comparendoNegReinService
	 * 
	 * @return
	 */
	public IComparendoNegReinService getComparendoNegReinService() {
		return comparendoNegReinService;
	}

	/**
	 * establecimiento del servicio comparendoNegReinService, la inyeccion de la
	 * dependencia la cubre SpringFramework
	 * 
	 * @param comparendoNegReinService
	 */
	public void setComparendoNegReinService(
			IComparendoNegReinService comparendoNegReinService) {
		this.comparendoNegReinService = comparendoNegReinService;
	}

	// --------------------------------------------------------------------
	// --- getters and setters end()
	// --------------------------------------------------------------------

}
